<?php
/*
 * 优惠设置
 */

namespace Common\Model;
use Common\Model\BaseModel;



class ConfigModel extends BaseModel{

    const PORT_MONEY        = 400;        //默认每月每端口400元

    /*
     * 每月费用
     * $port            //端口个数
     *
     * 返回数据
     * [
     *      'month_total_prices' => 2000,       //月（原价）
     *      'month_pay_prices' => 2000,       //月（折后价）
     *      'quarter_total_prices' => 6000,     //季度（原价）
     *      'quarter_pay_prices' => 6000,     //季度（折后价）
     *      'year_total_prices' => 2400        //年（原价）
     *      'year_pay_prices' => 2400        //年（折后价）
     * ]
    */
    public function getMonthMoney($port){
        $config = M("config")->where('status=1')->order('id desc')->find();

        $month_money = isset($config['money']) && !empty($config['money']) ?$config['money'] :ConfigModel::PORT_MONEY;//每月单个端口费用
        $month_money = $month_money * $port;//单月费用*端口

        $discounts = isset($config['discounts'])?json_decode($config['discounts'],true):[];

        $quarter_num = 3;//一个季度几个月
        $year_num = 12;//一年几个月

        //月
        $month_pay_prices = $month_money;

        //季度
        $quarter_money = bcmul($month_money,$quarter_num,2);
        $quarter_pay_prices = $quarter_money;


        //年
        $year_money = bcmul($month_money,$year_num,2);
        $year_pay_prices = $year_money;

        /*------------判断是否打折,寻找最优惠折扣------------start*/
        if(!empty($discounts)){
            //用于存放该月份的折扣数据
            $month_arr = ['month'=>0,'discount'=>10];
            $quarter_arr = ['month'=>0,'discount'=>10];
            $year_arr = ['month'=>0,'discount'=>10];

            foreach($discounts as $k=>$v){
                if(
                    isset($v['count']) && is_numeric($v['count']) && $v['count'] >=0 &&
                    isset($v['discount']) && !empty($v['discount']) && is_numeric($v['discount'])
                ){

                    $dispose = $this->disposeDiscounts($v['discount']);//处理折扣小数点

                        //月，有可能输入0，既买几个月都享受折扣？
                        if(($v['count'] <= 1) && $month_arr['month'] <= $v['count']){
                            $month_arr = ['month'=>$v['count'],'discount'=>$dispose];
                        }

                        //季度
                        if($v['count'] <= $quarter_num && $quarter_arr['month'] < $v['count']){
                            $quarter_arr = ['month'=>$v['count'],'discount'=>$dispose];
                        }

                        //年
                        if($v['count'] <= $year_num && $year_arr['month'] < $v['count']){
                            $year_arr = ['month'=>$v['count'],'discount'=>$dispose];;
                        }


                }
            }


            /*------------计算优惠价格------------start*/
            if(isset($month_arr['discount']) && $month_arr['discount'] != 10){
                $month_pay_prices = bcmul($month_pay_prices,$month_arr['discount'],2);//开始打折，保留2位小数
            }

            if(isset($quarter_arr['discount']) && $quarter_arr['discount'] != 10){
                $quarter_pay_prices = bcmul($quarter_pay_prices,$quarter_arr['discount'],2);//开始打折，保留2位小数
            }

            if(isset($year_arr['discount']) && $year_arr['discount'] != 10){
                $year_pay_prices = bcmul($year_pay_prices,$year_arr['discount'],2);//开始打折，保留2位小数
            }
            /*------------计算优惠价格------------end*/
        }
        /*------------判断是否打折,寻找最优惠折扣------------end*/

        $data = [];

        //一天一端口价格,月费用计算每天费用,按30天一个月来算
        $data['day_total_prices'] = bcdiv($month_money,30,2);

        //月
        $data['month_total_prices'] = $month_money;//原价
        $data['month_pay_prices'] = $month_pay_prices;//折后价

        //季度
        $data['quarter_total_prices'] = $quarter_money;//原价
        $data['quarter_pay_prices'] = $quarter_pay_prices;//折后价

        //年
        $data['year_total_prices'] = $year_money;//原价
        $data['year_pay_prices'] = $year_pay_prices;//折后价

        return $data;
    }


    /*
     * 处理折扣
     */
    public function disposeDiscounts($discounts){
        //如果不是数字或者小于等于0又或者大于10，都按原价算
        if(empty($discounts) || !is_numeric($discounts) || $discounts <= 0 || $discounts >= 10){
            return 10;
        }

        /*
         *  用户输入折扣的可能
         *
         * 俩位数
         *  7.5
         *  0.75
         *
         * 一位数
         *  7
         *  0.7
         */

        //如果小于1,既0.75或者0.7，用户输入正确，直接返回
        if($discounts<1){
            return $discounts;
        }

        $discounts = bcmul($discounts,0.1,2);

        return $discounts;
    }

}